Option Explicit On

Imports System
Imports System.Reflection
Imports System.ComponentModel

<ToolboxBitmap(GetType(DateTimePicker))> _
Public Class ACMaskedDateTime
   Inherits Control

   Public WithEvents ACMaskedTextBox As MaskedTextBox
   Public WithEvents ACCalendar As Button
   'Public NSCalendarControl As MonthCalendar
   Public Event OnACCalendar_Click(ByVal sender As Object, ByVal e As EventArgs)
   Public Event OnACCalendar_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
   Public Event ValueChanged(ByVal sender As Object, ByVal e As EventArgs)

   Public Sub New()
      MyBase.New()

      MyBase.Size = New System.Drawing.Size(100, 21)

      ACMaskedTextBox = New System.Windows.Forms.MaskedTextBox
      ACMaskedTextBox.Location = New System.Drawing.Point(325, 9)
      ACMaskedTextBox.Name = "ACMaskedTextBox"
      ACMaskedTextBox.Size = New System.Drawing.Size(100, 21)
      ACMaskedTextBox.Mask = "00/00/0000"
      ACMaskedTextBox.Dock = DockStyle.Top

      ACCalendar = New System.Windows.Forms.Button
      ACCalendar.Image = Global.ACControles.My.Resources.Resources.Calendar
      ACCalendar.UseVisualStyleBackColor = True
      ACCalendar.Size = New System.Drawing.Size(21, 21)
      ACCalendar.Dock = DockStyle.Right
      ACCalendar.Cursor = Cursors.Arrow
      ACCalendar.TabStop = False

      AddHandler ACCalendar.Click, AddressOf ACCalendar_Click

      'NSCalendarControl = New System.Windows.Forms.MonthCalendar
      'NSCalendarControl.Location = New System.Drawing.Point(0, 0)
      'NSCalendarControl.Name = "NSCalendarControl"
      'NSCalendarControl.Visible = True
      'NSCalendarControl.Dock = DockStyle.Fill

      ACMaskedTextBox.Controls.Add(ACCalendar)
      ACCalendar.BringToFront()

      MyBase.Controls.Add(ACMaskedTextBox)
      ACMaskedTextBox.BringToFront()

      'MyBase.Controls.Add(NSCalendarControl)
      'NSCalendarControl.BringToFront()

      'MyBase.MaximumSize = New System.Drawing.Size(NSCalendarControl.Width, 21)
   End Sub


   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
      MyBase.Dispose(disposing)
   End Sub
   Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
      MyBase.OnGotFocus(e)
   End Sub
   Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
      MyBase.OnTextChanged(e)
   End Sub
   Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
      Const WM_KEYDOWN As Integer = 256
      Const WM_SYSKEYDOWN As Integer = 260
      If ((msg.Msg = WM_KEYDOWN) _
                  OrElse (msg.Msg = WM_SYSKEYDOWN)) Then
         Select Case (keyData)
            Case Keys.Tab
               Dim e As New KeyEventArgs(Keys.Tab)
               MyBase.OnKeyDown(e)
         End Select
      End If
      Return MyBase.ProcessCmdKey(msg, keyData)
   End Function

   Protected domainUpDown1 As DomainUpDown

   Protected Sub ACCalendar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ACCalendar.Click
      Dim ACCalendarControl As MonthCalendar
      ACCalendarControl = New System.Windows.Forms.MonthCalendar
      ACCalendarControl.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y)
      ACCalendarControl.Name = "ACCalendarControl"
      ACCalendarControl.Visible = True
      AddHandler ACCalendarControl.DateChanged, AddressOf ACCalendarControl_DateChanged

      Me.Parent.Controls.Add(ACCalendarControl)
      ACCalendarControl.BringToFront()



      'Me.MaximumSize = New System.Drawing.Size(NSCalendarControl.Width, NSCalendarControl.Height + 21)
      'Me.Size = New System.Drawing.Size(NSCalendarControl.Width, NSCalendarControl.Height + 21)


   End Sub

   Private Sub ACCalendarControl_DateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs)
      Me.ACMaskedTextBox.Text = CType(sender, MonthCalendar).SelectionStart.ToString(Me.ACFormatos.ddMMyyyy.ToString())
      CType(sender, MonthCalendar).Parent.Controls.Remove(CType(sender, MonthCalendar))
   End Sub


   Public Overrides Property Text() As String
      Get
         Return Me.ACMaskedTextBox.Text
      End Get
      Set(ByVal value As String)
         Me.ACMaskedTextBox.Text = value
      End Set
   End Property
   Public Overrides Property Cursor() As System.Windows.Forms.Cursor
      Get
         Return MyBase.Cursor
      End Get
      Set(ByVal value As System.Windows.Forms.Cursor)
         MyBase.Cursor = value
      End Set
   End Property

   Public Shadows Property Tag() As Object
      Get
         Return MyBase.Tag
      End Get
      Set(ByVal value As Object)
         MyBase.Tag = value
      End Set
   End Property

   Public Property Mask() As String
      Get
         Select Case m_formato
            Case ACFormatos.ddMMyyyy
               Return "00/00/0000"
            Case ACFormatos.MMddyyyy
               Return "00/00/0000"
         End Select
         Return Me.ACMaskedTextBox.Mask
      End Get
      Set(ByVal value As String)
         Me.ACMaskedTextBox.Mask = value
      End Set
   End Property

   Private DropDown As EventHandler

   Protected Overridable Sub OnDropDown(ByVal eventargs As EventArgs)
      If (Not Me.DropDown Is Nothing) Then
         Me.DropDown.Invoke(Me, eventargs)
      End If
   End Sub



   Public Property Value() As Date
      Get
         If IsDate(ACMaskedTextBox.Text) Then
            Return CType(ACMaskedTextBox.Text, Date)
         Else
            Return Nothing
         End If
      End Get
      Set(ByVal value As Date)
         ACMaskedTextBox.Text = value.ToString(Me.Formato.ToString())
      End Set
   End Property

   'm.e = New Hashtable
   ' m.e.Add("d", dateTimeFormat.ShortDatePattern)
   ' m.e.Add("D", dateTimeFormat.LongDatePattern)
   ' m.e.Add("t", dateTimeFormat.ShortTimePattern)
   ' m.e.Add("T", dateTimeFormat.LongTimePattern)
   ' m.e.Add("f", (dateTimeFormat.LongDatePattern & " " & dateTimeFormat.ShortTimePattern))
   ' m.e.Add("F", dateTimeFormat.FullDateTimePattern)
   ' m.e.Add("g", (dateTimeFormat.ShortDatePattern & " " & dateTimeFormat.ShortTimePattern))
   ' m.e.Add("G", (dateTimeFormat.ShortDatePattern & " " & dateTimeFormat.LongTimePattern))
   ' m.e.Add("M", dateTimeFormat.MonthDayPattern)
   ' m.e.Add("R", dateTimeFormat.RFC1123Pattern)
   ' m.e.Add("s", dateTimeFormat.SortableDateTimePattern)
   ' m.e.Add("u", dateTimeFormat.UniversalSortableDateTimePattern)
   ' m.e.Add("U", dateTimeFormat.FullDateTimePattern)


   Public Enum ACFormatos
      ddMMyyyy = 0
      MMddyyyy = 1
   End Enum
   Private m_formato As ACFormatos
   Public Property Formato() As ACFormatos
      Get
         Return m_formato
      End Get
      Set(ByVal value As ACFormatos)
         m_formato = value
      End Set
   End Property


End Class
